package com.glela.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.glela.platform.mapper.UserMoneySnapshotMapper;
import com.glela.platform.model.DumpConditions;
import com.glela.statistics.mapper.OrderStatisticsMapper;
import com.glela.talent.util.MyDateUtil;

/**
 * <dl>
 * <dt>用户妆币快照定时任务</dt>
 * </dl>
 * 
 * @param orderObjectList
 * @author gaoweiqi
 * @date 2017年8月31日 12:44:05
 */
public class UserMoneySnapshotTask {
	private static final Logger logger = LoggerFactory.getLogger(UserMoneySnapshotTask.class);
	
	@Autowired
	private UserMoneySnapshotMapper userMoneySnapshotMapper;
	@Autowired
	private OrderStatisticsMapper orderStatisticsMapper;
	
	private MyDateUtil util = MyDateUtil.INSTANCE;
	
	/**
	* 00:00:00 自动复制用户余额和妆币
	* @author gaoweiqi
	* @date 2017年9月2日 12:44:05
	*
	**/
	public void autoCopyUserMoney() {
		logger.info("------------>Begin Snapshot");
		try {
			userMoneySnapshotMapper.insertIntoCUserSyc();
		} catch (Exception e) {
			logger.error("------->妆币余额复制异常", e);
		}
		logger.info("------------>End Snapshot");
	}

	public void autoSnapshot(){
		try {
			logger.info("------------>Begin Snapshot");
			
			DumpConditions conditions = new DumpConditions();
			
			conditions.setStartDate(util.getOldDays(1));
			conditions.setEndDate(util.getYesterday(new Date()));
			
			conditions.setStartQuarter(util.getOldDays(90));
			conditions.setEndQuarter(util.getYesterday(new Date()));
			
			userMoneySnapshotMapper.insertYesterDayData(conditions);
			logger.info("------------>Begin Snapshot");
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("------->妆币 余额 快照异常", e);
		}
	}
	public void validationMoney(){
		Map<Object,Object> map=new HashMap<Object,Object>();
		try { 
		//验证所有佣金和订单是否匹配
		List<Map<Object, Object>> OrderObjMap=orderStatisticsMapper.validationCommision();
		map.put("ExceptionOrderObj", OrderObjMap);

		//验证处理后的佣金和流水是否匹配
		List<Map<Object, Object>> JournalMap=orderStatisticsMapper.validationJournal();
		map.put("ExceptionJournal", JournalMap);
//		 int i=Integer.MAX_VALUE;
		//验证用户余额和流水不一致
		List<Map<Object, Object>> MoneyMap=orderStatisticsMapper.validationUserMoney();
		map.put("ExceptionMoney", MoneyMap);
		//验证账户余额变化过程异常(某一时刻 期初减期末成为负数)
		List<Map<Object, Object>> MoneyProcessMap=orderStatisticsMapper.validationUserMoneyProcess();
		long userID=0L;//当前正在计算中的用户ID
		long ErrorUserID=0L;//当前流水发生错误的用户ID
		int  UserMoney=0;
		List<Map<Object, Object>> MoneyProcessErrorMap=new ArrayList<Map<Object, Object>>();
		for(Map<Object, Object> temp:MoneyProcessMap){ 
			long TempUserID=temp.get("user_id")==null?0:Long.parseLong(temp.get("user_id").toString());
			int TempMoney=temp.get("money")==null?0:Integer.parseInt(temp.get("money").toString());
			String Flag=temp.get("type")==null?"":temp.get("type").toString();
			if(TempUserID==0L||TempUserID==0||!"+-".contains(Flag)){
				logger.error("------->此流水异常");
				continue;
			}
			if(ErrorUserID==TempUserID){//当前的用户上一条流水已经发生错误了
				continue;
			}
			
			if(TempUserID!=userID){//开始判断下一个用户的流水 重置数据
				userID=TempUserID;
				UserMoney=0;
			}
			//但比计算逻辑
			if("+".equals(Flag)){
				UserMoney+=TempMoney;
			}else{
				UserMoney-=TempMoney;
			}
			//当前的流水异常
			if(UserMoney<0){
				ErrorUserID=TempUserID;
				MoneyProcessErrorMap.add(temp);
			}
	map.put("ExceptionProcessMoney", MoneyProcessErrorMap);
		}   
		} catch (Exception e) {
			logger.error("------->validationMoney异常", e);
		}
	}
}
